package com.example.rabbit.three;

import com.example.rabbit.util.RabbitMqUtils;
import com.example.rabbit.util.SleepUtil;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

/**
 * 队列模式
 * 1.不自动应答
 * 2.不公平分发
 **/
public class Work021 {
    public static void main(String[] args) throws IOException, TimeoutException {
        Channel channel = RabbitMqUtils.getChannel();
        DeliverCallback deliverCallback = (consumeTag, delivery) -> {
            SleepUtil.sleep(1);
            System.out.println("接收消息" + consumeTag + "====" + new String(delivery.getBody()));
            channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        };
        CancelCallback cancelCallback = consumeTag -> {
            System.out.println("取消" + consumeTag);
        };
        System.out.println("工作线程C1等待。。。");

        // 不公平分发 预取值
        channel.basicQos(2);

        String queueName = "ack_queue";
        boolean autoAsk = false;
        channel.basicConsume(queueName, autoAsk, deliverCallback, cancelCallback);

    }
}
